/**
 * My simple header template from child directory
 * ConcurrentTreasureHunter.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
package FourthTry.concurrent;

/*
 * #%L
 * Ability-tester
 * %%
 * Copyright (C) 2014 AmazingSERKU
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import FourthTry.Cell;
import FourthTry.ICoordinator;
import FourthTry.TreasureHunter;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 21.01.14
 * Time: 21:40
 * To change this template use File | Settings | File Templates.
 */
public class ConcurrentTreasureHunter extends TreasureHunter implements Runnable {

    private int power;
    private ConcurrentExecutor executor;
    private CyclicBarrier barrier;

    public ConcurrentTreasureHunter(int power) {
        super();
        this.power = power;
    }

    @Override
    protected StepResult takeAStep() {
        StepResult result = analyze();
        if (result!=null) {
            return result;
        }
        try {
            barrier.await();
        }   catch (InterruptedException ignored) {}
            catch (BrokenBarrierException ignored) {}
        interact();
        return StepResult.STEP_MADE;
    }

    @Override
    public void run() {
        removeHimself();
    }

    public Cell getCell() {
        return goal;
    }

    public void setCell(Cell cell) {

    }

    public ConcurrentExecutor getExecutor() {
        return executor;
    }

    public void setExecutor(ConcurrentExecutor executor) {
        this.executor = executor;
    }

    public void setBarrier(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    public int getPower() {
        return power;
    }

    private void removeHimself() {
        this.executor.removeHunter(this);
    }
}
